import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.EventObject;

import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;


public class SettingsManager extends EventObject{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JFileChooser chooser;
	private String userName;
	private JComboBox<String> box;

	public SettingsManager(Object source,String eventName, String userName, JComboBox com) {
		super(source);
		this.userName = userName;
		box = com;
		chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		
        if (eventName.equals("addBird"))  addBird();
		if (eventName.equals("addObstacle"))  addObstacle();
		if (eventName.equals("addBonus"))  addBonus();
		if (eventName.equals("fillBirds"))  fillBirds();
		if (eventName.equals("fillObstacles"))  fillObstacles();
		if (eventName.equals("fillBonuses"))  fillBonuses();
		if (eventName.equals("changeBird")) changeBird();
		if (eventName.equals("changeObstacle")) changeObstacle();
		if (eventName.equals("changeBonus")) changeBonus();
	}
	
	private void addBird(){
		addImage("bird");
	}
	
	private void changeBird(){
		getImage("bird");
	}
	private void changeObstacle(){
		getImage("obstacle");
	}
	private void changeBonus(){
		getImage("bonus");
	}
	
	private void fillBirds(){
		fillImages("bird");
	}
	
	private void addObstacle(){
		addImage("obstacle");
	}
	private void fillObstacles(){
		fillImages("obstacle");
	}

	private void addBonus(){
		addImage("bonus");
	}
	private void fillBonuses(){
		fillImages("bonus");
	}
	
	private String doubleBackSlash(String str){
		String returnStr = str.replace("\\", "\\\\");
		return returnStr;
	}
	private void addImage(String type){
		int returnVal = chooser.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
	        DatabaseManager.openConnection();
			Statement stmt = null;
			String sql;
			try {
				String name = doubleBackSlash(chooser.getSelectedFile().getPath());
				String imageName = chooser.getSelectedFile().getName();
				sql = "insert into flappybirdv2db.imagepath (imagePath, userName, imageName, typeImage) values ('"+name+"','"+userName+"', '" + imageName + "', '"+type+"')";
				stmt = DatabaseManager.conn.createStatement();
				stmt.executeUpdate(sql);
		        box.addItem(imageName);
				
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			DatabaseManager.closeConnection();     
        }

	}
	private void getImage(String type){
		DatabaseManager.openConnection();
		Statement stmt = null;
		String sql;
		try {
			System.out.print(box.getSelectedItem().toString());
			sql = "Select * from flappybirdv2db.imagepath where userName = '"+userName+"' and typeImage='"+type+"' and imageName = '"+box.getSelectedItem().toString() +"'";
			stmt = DatabaseManager.conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				if(type.equals("bird"))
					GameObjectPanel.setBirdPath(rs.getString("imagePath"));
				else if(type.equals("obstacle"))
					GameObjectPanel.setObstaclePath(rs.getString("imagePath"));
				else if(type.equals("bonus"))
					GameObjectPanel.setBonusPath(rs.getString("imagePath"));
				else
					JOptionPane.showMessageDialog(null, "Wrong Path!", "ERROR", JOptionPane.ERROR_MESSAGE);	
			}
			
			sql = "update flappybirdv2db.imagepath set dateadded = CURRENT_TIMESTAMP where userName = '"+userName+"' and typeImage='"+type+"' and imageName = '"+box.getSelectedItem().toString() +"'";
			stmt = DatabaseManager.conn.createStatement();
			stmt.executeUpdate(sql);

		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		DatabaseManager.closeConnection();
	}
	private void fillImages(String type){
		DatabaseManager.openConnection();
		Statement stmt = null;
		try {
			String sql = "Select * from flappybirdv2db.imagepath where userName = '"+ userName +"' and typeImage = '"+type+"'";
			stmt = DatabaseManager.conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				box.addItem(rs.getString("imageName"));
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		DatabaseManager.closeConnection();
	}
}
